<?php

namespace app\model\v1\recruit;

use crmeb\traits\ModelTrait;
use think\Model;

class RecruitJobSeeker extends Model
{

    use ModelTrait;

    protected $updateTime = false;
    protected $createTime = false;

    protected $hidden = [
    //    'create_time','update_time','delete_time'
    ];


    public function workingyears ()
    {
        return $this->hasOne(RecruitJobCate::class,'code','working_years');
    }
    public function degrees ()
    {
        return $this->hasOne(RecruitJobCate::class,'code','degree');
    }
    public function userjobstatus ()
    {
        return $this->hasOne(RecruitJobCate::class,'code','user_job_status');
    }

    public function Collection ()
    {
        return $this->hasOne (RecruitCollection::class,'job_id','id');
    }
    public function industry ()
    {
        return $this->hasOne(RecruitIndustry::class,'id','industry_id');
    }


    /**
     * 求职招聘后台 - 获取人才管理列表
     * @param $param
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getJobUserList($param = [])
    {
        $data["data"] = self::field("id,user_avatar,user_name,working_years,degree,school,user_job_status,phone,status,
            case when status=1 then '上架'
            when status =2 then '下架'
            end as status_text
            ")
            ->whereNULL("delete_time")
            ->where(function ($query) use ($param){
                if(!empty($param["user_name"])) $query->where("user_name", 'like', '%'.$param["user_name"].'%');   //根据word搜索
                if(!empty($param["degree"])) $query->where("degree", $param["degree"]);   //根据学历搜索
                if(!empty($param["working_years"])) $query->where("working_years", $param["working_years"]);   //根据工作年限搜索
                if(!empty($param["status"])) $query->where("status", $param["status"]);   //根据状态搜索
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('create_time', 'between', [strtotime($wheretime[0]),strtotime($wheretime[1])]);
                }
            })
            ->page($param["page"], $param["limit"])
            ->select()
            ->toArray();

        $data["count"] = self::field("id,user_avatar,user_name,working_years,degree,school,user_job_status,phone,status")
            ->whereNULL("delete_time")
            ->where(function ($query) use ($param){
                if(!empty($param["user_name"])) $query->where("user_name", 'like', '%'.$param["user_name"].'%');   //根据word搜索
                if(!empty($param["degree"])) $query->where("degree", $param["degree"]);   //根据degree搜索
                if(!empty($param["working_years"])) $query->where("working_years", $param["working_years"]);   //根据工作年限搜索
                if(!empty($param["status"])) $query->where("status", $param["status"]);   //根据状态搜索
            })
            ->count();

        return $data;
    }


    /**
     * 求职招聘后台 - 获取人才管理列表 - 获取搜索项数据
     * @param $param
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getJobUserData($param = [])
    {
        $data["degree_data"] = RecruitJobCate::where("type", 2)->select()->toArray();  //学历下拉列表
        $data["workyear_data"] = RecruitJobCate::where("type", 1)->select()->toArray();   //工作经验下拉列表

        return $data;
    }




    /**
     * 求职招聘后台 - 获取人才管理列表 - 查看
     * @param $param
     * @return RecruitJobSeeker|array|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getJobUserDetail($param = [])
    {
        return self::field("*,
            case when status=1 then '上架'
            when status =2 then '下架'
            end as status_text
            ")
            ->with([
                'industry'=> function($query) {
                    $query->field ('id,industry_name');
                }
            ])
            ->where("id", $param["id"])
            ->find();
    }


    /**
     * 求职招聘后台 - 获取人才管理列表 - 启用禁用
     * @param $param
     * @return string
     */
    public static function enableJobUser($param = [])
    {
        //获取用户发送的id，根据状态修改当前banner的启用禁用状态
        $status = self::where("id", $param['id'])->value("status");
        $msg = '';
        if($status == 1){   //1启用 2禁用
            $enable = 2;
            $msg = "禁用成功";
        }else{
            $enable = 1;
            $msg = "启用成功";
        }
        self::where("id", $param['id'])->update(['status' => $enable]);
        return $msg;
    }





}
